
local skynet = require "skynet"

local function get_up(f)
    local u = {}
    if not f then
        return u
    end
    local i = 1
    while true do
        local name, value = debug.getupvalue(f, i)
        if name == nil then
            return u
        end
        print("name="..name.." value:"..tostring(value))
        u[name] = value
        i = i + 1
    end
    return u
end

print("1111111111111111111111111")
local temp =  get_up(_G.update_table)
local table_db = temp["table_db"]
if table_db ==  nil then
    print("find table_db a nil")
end

local user_db = temp["user_db"]
if user_db ==  nil then
    print("find user_db a nil")
end


_G.update_table = function(sz,msg)
local table_id,status,user_num=string.unpack("I4I1I1", msg, 5)
local user={}
local flag=0
for i=1,user_num do
    local user_id=string.unpack("I4",msg,11+flag)
    table.insert(user,user_id)
    flag=flag+4
end

if not table_db[table_id] then
    return
end

if not user then
    return
end

if status==0 then     --妗屽瓙瑙ｆ暎
    skynet.error("table has dismiss table_id: " ,table_id)
    for k,v in pairs(user) do
        if user_db[v] then 
            user_db[v].table_id=0
            skynet.error("table has dismiss table_id: " ,table_id," user_id: ",v)
        end
    end
    table_db[table_id]=nil

elseif status==1 then --娓告垙寮??濮嬶紝鏇存柊妗屽瓙淇℃伅
    table_db[table_id].user=user
    table_db[table_id].lasttime=skynet.time()
    for k,v in pairs(user) do
        if user_db[v] then 
            user_db[v].table_id=table_id
            skynet.error("game start table update user table_id : " ,table_id," user_id: ",v)
        end
    end

elseif status==2 then --鏈変汉杩涘叆锛屾洿鏂版??屽瓙淇℃伅
    for k,v in pairs(user) do
        if user_db[v] then 
            user_db[v].table_id=table_id
            table.insert(table_db[table_id].user,v)
            skynet.error("user enter table  table_id: ",table_id," user_id: ",v)
        end
    end

elseif status==3 then --鏈変汉閫??鍑猴紝鏇存柊妗屽瓙淇℃伅
    for k,v in pairs(user) do
        if user_db[v] then 
            user_db[v].table_id=0
            for key,vaule in pairs(table_db[table_id].user) do
                if vaule==v then
                    table_db[table_id].user[key]=nil
                    skynet.error("user out table  table_id : ",table_id," user_id: ",v)
                end
            end
        end
    end
end
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               end
